package com.hqyj.javaSpringCloudAccount.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.entity.account.Resource;
import com.hqyj.entity.account.RoleResource;
import com.hqyj.entity.common.Result;
import com.hqyj.entity.common.SearchBean;
import com.hqyj.javaSpringCloudAccount.dao.ResourceDao;
import com.hqyj.javaSpringCloudAccount.dao.RoleResourceDao;
import com.hqyj.javaSpringCloudAccount.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private RoleResourceDao roleResourceDao;

    @Override
    public Resource getResourceById(int id) {
        return resourceDao.getResourceById(id);
    }

    @Override
    public PageInfo<Resource> getResourceBySearchBean(SearchBean searchBean) {
        // 初始化分页列表，若前端无值传回则返回默认值
        searchBean.initSearchBean();

        // 开始分页
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Resource>(Optional
                .ofNullable(resourceDao.getResourceByVo(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    @Transactional
    public Result<Resource> insertResource(Resource resource) {
        resource.setCreateDate(LocalDateTime.now());
        resourceDao.insertResource(resource);

        // 在 account_role_resource 表中添加记录
        if (resource.getRoles() != null) {
            resource.getRoles().stream().forEach(item -> {
                roleResourceDao.insertRoleResource(new RoleResource(resource.getId(), item.getId()));
            });
        }
        return new Result<Resource>(Result.ResultCode.SUCCESS.code, "Insert Success.", resource);
    }

    @Override
    @Transactional
    public Result<Resource> updateResource(Resource resource) {
        resourceDao.updateResource(resource);

        // 若有对角色更新权限时，更新 account_role_resource 表重设权限
        roleResourceDao.deleteRoleResourceByResourceId(resource.getId());
        if (resource.getRoles() != null) {
            resource.getRoles().stream()
                    .forEach(item -> {
                        roleResourceDao.insertRoleResource(new RoleResource(resource.getId(), item.getId()));
                    });
        }
        return new Result<Resource>(Result.ResultCode.SUCCESS.code, "Update Success.", resource);
    }

    @Override
    @Transactional
    public Result<Object> deleteResourceById(int id) {
        resourceDao.deleteResourceById(id);
        roleResourceDao.deleteRoleResourceByResourceId(id);
        return new Result<Object>(Result.ResultCode.SUCCESS.code, "Delete Success.");
    }

    @Override
    public List<Resource> getResourcesByRoleId(Integer roleId) {
        return resourceDao.getResourcesByRoleId(roleId);
    }
}
